/*
 * www.unisinsight.com Inc.
 * Copyright (c) 2019 All Rights Reserved.
 */

/*
 * 修订记录：
 * huangxing 2019年7月26日 上午9:31:46 创建
 */

package com.fengwuj.mutils.data;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;

/**
 * 修订记录：
 * jiangbo 2020/1/7 19:11 创建
 *
 * @author jiangbo
 */
@Slf4j
public class AESUtil {
	/** 算法/模式/填充 **/
	private static final String CipherMode = "AES/CBC/NoPadding";
    /** 秘钥key,16位字符串 **/
	private static final String SECRETKEY = "asidofit96fwi756";
    /** 偏移量，16位数字 **/
    private static final String IV = "7156549587632412";
	
	/** 加密字节数据 **/
	public static byte[] encrypt(byte[] content, String secretKey, String iv) {
		try {
			Cipher cipher = Cipher.getInstance(CipherMode);
			int blockSize = cipher.getBlockSize();
			int plaintextLength = content.length;
			if (plaintextLength % blockSize != 0) {
				plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize));
			}
			byte[] plaintext = new byte[plaintextLength];
			System.arraycopy(content, 0, plaintext, 0, content.length);
			SecretKeySpec keyspec = new SecretKeySpec(secretKey.getBytes(), "AES");
			IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());
			cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
			byte[] result = cipher.doFinal(plaintext);
			return result;
		} catch (Exception e) {
			log.info(e.getMessage(), e);
		}
		return null;
	}
	
	/** 加密(BASE64编码) **/
	public static String encrypt(String content, String secretKey, String iv) {
		byte[] data = null;
		try {
			data = content.getBytes("UTF-8");
		} catch (Exception e) {
			log.info(e.getMessage(), e);
		}
		if(data == null){
			return null;
		}
		data = encrypt(data, secretKey, iv);
		return Base64.encodeBase64String(data);
	}

    /** 加密(BASE64编码) **/
    public static String encrypt(String content) {
        return encrypt(content,SECRETKEY,IV);
    }
	
	/** 解密字节数组 **/
	public static byte[] decrypt(byte[] content, String secretKey, String iv) {
		try {
			Cipher cipher = Cipher.getInstance(CipherMode);
			int blockSize = cipher.getBlockSize();
			int plaintextLength = content.length;
			if (plaintextLength % blockSize != 0) {
				plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize));
			}
			byte[] plaintext = new byte[plaintextLength];
			System.arraycopy(content, 0, plaintext, 0, content.length);
			
			SecretKeySpec keyspec = new SecretKeySpec(secretKey.getBytes(), "AES");
			IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());
			cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);
			byte[] result = cipher.doFinal(plaintext);
			return result;
		} catch (Exception e) {
			log.info(e.getMessage(), e);
		}
		return null;
	}
	
	/** 解密 **/
	public static String decrypt(String content, String secretKey, String iv) {
		byte[] data = null;
		try {
			data = Base64.decodeBase64(content);
		} catch (Exception e) {
			log.info(e.getMessage(), e);
		}
		if(data == null){
			return null;
		}
		data = decrypt(data, secretKey, iv);
		String result = "";
		try {
			result = new String(data, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			log.info(e.getMessage(), e);
		}
		return result.trim();
	}

    /** 加密(BASE64编码) **/
    public static String decrypt(String content) {
        return decrypt(content,SECRETKEY,IV);
    }
	
}
